package com.dick.base.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BusinessRollAllMapper;
import com.dick.base.mapper.BusinessRollInfoMapper;
import com.dick.base.mapper.BusinessRollMapper;
import com.dick.base.model.BusinessRoll;
import com.dick.base.model.BusinessRollAll;
import com.dick.base.model.BusinessRollInfo;
import com.dick.base.parameter.RollAddParameter;
import com.dick.base.parameter.RollModifyParameter;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库存管理
 */
@Service
public class RollService {

    @Autowired
    private BusinessRollMapper businessRollMapper;
    @Autowired
    private BusinessRollInfoMapper businessRollInfoMapper;
    @Autowired
    private BusinessRollAllMapper businessRollAllMapper;

    /**
     * 卷分页查询
     * @param roll
     * @param pageInfo
     * @return
     */
    @SmartQuery(clazz = BusinessRollAll.class, likeFields = {"batchNumber", "name", "supplyCompany", "supplyTruck"})
    public Page<BusinessRollAll> rollPage(BusinessRollAll roll, PageInfo pageInfo) {
        return businessRollAllMapper.selectPage(SmartQueryUtil.getPage(), SmartQueryUtil.getQueryWrapper());
    }

    /**
     * 卷新增
     * @param parameter
     */
    @Transactional
    public void rollAdd(RollAddParameter parameter) {
        BusinessRollInfo rollInfo = new BusinessRollInfo();
        rollInfo.setId(IdUtil.nextId());
        rollInfo.setPrice(parameter.getInfo().getPrice());
        rollInfo.setCreateTime(DateUtil.now());
        rollInfo.setSupplyDate(DateUtil.parseDate(parameter.getInfo().getSupplyDate()));
        rollInfo.setSupplyCompany(parameter.getInfo().getSupplyCompany());
        rollInfo.setSupplyTruck(parameter.getInfo().getSupplyTruck());
        List<BusinessRoll> rolls = parameter.getRolls().stream().map(roll -> {
            BusinessRoll businessRoll = new BusinessRoll();
            businessRoll.setId(IdUtil.nextId());
            businessRoll.setBatchNumber(roll.getBatchNumber());
            businessRoll.setName(roll.getName());
            businessRoll.setInitWeight(roll.getInitWeight());
            businessRoll.setPrice(rollInfo.getPrice());
            businessRoll.setMaterial(roll.getMaterial());
            businessRoll.setThickness(roll.getThickness());
            businessRoll.setWidth(roll.getWidth());
            businessRoll.setBalanceWeight(businessRoll.getInitWeight());
            businessRoll.setPurchaseDate(rollInfo.getSupplyDate());
            businessRoll.setCreateTime(DateUtil.now());
            businessRoll.setStatus(BusinessRoll.STATUS_AVAILABLE);
            businessRoll.setTotalPrice(businessRoll.getPrice().multiply( businessRoll.getInitWeight()));
            businessRoll.setInfoId(rollInfo.getId());
            return businessRoll;
        }).collect(Collectors.toList());
        rolls.stream().forEach(businessRollMapper::insert);
        businessRollInfoMapper.insert(rollInfo);
    }

    /**
     * 卷修改
     * @param parameter
     */
    public void rollModify(RollModifyParameter parameter) {
//        BusinessRoll roll = businessRollMapper.selectOne(
//                new QueryWrapper<BusinessRoll>().eq("batch_number", parameter.getBatchNumber()));
//        if (roll != null && !roll.getId().equals(parameter.getId())) {
//            throw new BaseRuntimeException(String.format("该批号已被使用：%s", parameter.getBatchNumber()), HttpStatus.BAD_REQUEST);
//        }
        BusinessRoll businessRoll = new BusinessRoll();
        businessRoll.setId(parameter.getId());
//        businessRoll.setBatchNumber(parameter.getBatchNumber());
//        businessRoll.setName(parameter.getName());
//        businessRoll.setInitWeight(parameter.getInitWeight());
//        businessRoll.setPrice(parameter.getPrice());
//        businessRoll.setSupplyCompany(parameter.getSupplyCompany());
//        businessRoll.setMaterial(parameter.getMaterial());
//        businessRoll.setThickness(parameter.getThickness());
//        businessRoll.setWidth(parameter.getWidth());
        businessRoll.setBalanceWeight(parameter.getBalanceWeight());
        businessRoll.setUpdateTime(DateUtil.now());
        if (!StringUtils.isEmpty(parameter.getStatus())) {
            businessRoll.setStatus(Integer.parseInt(parameter.getStatus()));
        }
        businessRollMapper.updateById(businessRoll);
    }
}
